/**
 * 
 */
package com.huawei.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.huawei.struts.form.VillageForm;
import com.huawei.struts.service.IVillageService;
import com.huawei.struts.util.PageBean;
import com.st.model.LjwVillageT;

/**
 * @author huganpu
 *
 */
public class VillageAction extends BaseAction {

	private static Logger logger=Logger.getLogger(VillageAction.class);
	
	private final String returnUrl="/village.do?method=init";
	
	private IVillageService VillageService;
	
	public void setVillageService(IVillageService villageService) {
		VillageService = villageService;
	}

	/**
	 * 初始化用户列表
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward init(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		VillageForm VillageForm =(VillageForm)form;
		logger.debug("into Village init method ...");
		VillageForm.setVillage(new LjwVillageT());//清空所有条件
		PageBean pageBean=VillageService.getVillageList(VillageForm.getVillage(), VillageForm.getPageBean());
		request.setAttribute("pageBean", pageBean);
		return mapping.findForward("toList");
	}
	
	public ActionForward search(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		VillageForm VillageForm =(VillageForm)form;
		logger.debug("into Village search method ...");
		PageBean pageBean=VillageService.getVillageList(VillageForm.getVillage(), VillageForm.getPageBean());
		request.setAttribute("pageBean", pageBean);
		return mapping.findForward("toList");
	}
	
	/**
	 * 跳转到用户创建页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toCreateVillage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		VillageForm VillageForm =(VillageForm)form;
		logger.debug("into Village toCreateVillage method ...");
		this.saveToken(request);
		return mapping.findForward("toAdd");
	}
	
	/**
	 * 创建用户
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward createVillage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		VillageForm VillageForm =(VillageForm)form;
		logger.debug("into Village createVillage method ...");
		if(this.isTokenValid(request,true))
		{
			VillageService.addVillage(VillageForm.getVillage());
			return super.goSuccess("操作成功", returnUrl, request, response, mapping);
		}
		else
		{
			return super.goFail("表单不能重复提交", returnUrl, request, response,mapping);
		}
	}
	
	/**
	 * 跳转到用户修改页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toUpdateVillage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		VillageForm VillageForm =(VillageForm)form;
		logger.debug("into Village toUpdateVillage method ...");
		Integer id=new Integer(request.getParameter("id"));
		VillageForm.setVillage(VillageService.getVillageInfoByid(id));
		this.saveToken(request);
		return mapping.findForward("toUpdate");
	}
	
	/**
	 * 修改用户
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward updateVillage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		VillageForm VillageForm =(VillageForm)form;
		logger.debug("into Village updateVillage method ...");
		if(this.isTokenValid(request,true))
		{
			VillageService.modifyVillage(VillageForm.getVillage());
			return super.goSuccess("操作成功", returnUrl, request, response, mapping);
		}
		else
		{
			return super.goFail("表单不能重复提交", returnUrl, request, response,mapping);
		}
	}
	
	public ActionForward delVillage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		VillageForm VillageForm =(VillageForm)form;
		Integer id=new Integer(request.getParameter("id"));
		VillageForm.getVillage().setVillageId(id);
		VillageService.delVillage(VillageForm.getVillage());
		return super.goSuccess("操作成功", returnUrl, request, response, mapping); 
	}
	
	public ActionForward selectVillage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		VillageForm VillageForm =(VillageForm)form;
		logger.debug("into Village init method ...");
		PageBean pageBean=VillageService.getVillageList(VillageForm.getVillage(), VillageForm.getPageBean());
		request.setAttribute("pageBean", pageBean);
		return mapping.findForward("toSelectVillage");
	}
	

}
